#!/bin/bash

# 定义变量
GO_VERSION="1.17.3"
#GO_DOWNLOAD_URL="https://golang.org/dl/go$GO_VERSION.linux-amd64.tar.gz"	#官网下载地址，可安装最新版本
#GO_DOWNLOAD_URL="https://go.dev/dl/go$GO_VERSION.linux-amd64.tar.gz"		#英文网下载地址，可安装最新版本 1.23.0
GO_DOWNLOAD_URL="https://go.p2hp.com/dl/go$GO_VERSION.linux-amd64.tar.gz"	#Go语言中文网，目前最新可安装 1.17.3 ,可用版本参考：https://go.p2hp.com/go.dev/dl/
INSTALL_DIR="/usr/local/go"

IS_GO111MODULE=on
#GOROOT_DIR=""
GOPATH_DIR_1=/data/go/${GO_VERSION}/workspace
GOPATH_DIR_2=/data/go/${GO_VERSION}/other
GOPROXY_URL="https://goproxy.cn"	#七牛云
GOCACHE_DIR=/data/go/${GO_VERSION}/gocache


# 检测包管理器类型
detect_package_manager() {
    if command -v yum &> /dev/null; then
        echo "Detected package manager: Yum"
        PACKAGE_MANAGER=yum
    elif command -v dnf &> /dev/null; then
        echo "Detected package manager: DNF"
        PACKAGE_MANAGER=dnf
    elif command -v apt &> /dev/null; then
        echo "Detected package manager: Apt"
        PACKAGE_MANAGER=apt
    else
        echo "Unsupported package manager or no package manager detected."
        exit 1
    fi
}

# 根据包管理器安装必要的软件
install_dependencies() {
    # 检查 tar 是否已安装
    if ! command -v tar &> /dev/null; then
        echo "tar not found. Installing..."
        case $PACKAGE_MANAGER in
            yum|dnf)
                sudo $PACKAGE_MANAGER install -y tar
                ;;
            apt)
                sudo apt-get update && sudo apt-get install -y tar
                ;;
            *)
                echo "Unknown package manager: $PACKAGE_MANAGER"
                exit 1
                ;;
        esac
    fi

    # 检查 curl 是否已安装
    if ! command -v curl &> /dev/null; then
        echo "curl not found. Installing..."
        case $PACKAGE_MANAGER in
            yum|dnf)
                sudo $PACKAGE_MANAGER install -y curl
                ;;
            apt)
                sudo apt-get update && sudo apt-get install -y curl
                ;;
            *)
                echo "Unknown package manager: $PACKAGE_MANAGER"
                exit 1
                ;;
        esac
    fi
}

# 下载Go安装包
download_go() {
    echo "Downloading Go $GO_VERSION..."
    curl -L -o go.tar.gz "$GO_DOWNLOAD_URL"
}

# 解压Go安装包
extract_go() {
    echo "Extracting Go..."
    sudo tar -C /usr/local -xzf go.tar.gz
}

# 清理下载的安装包
cleanup() {
    rm go.tar.gz
}

# 更新环境变量
update_environment() {
    echo "Updating environment variables..."
    if [ ! -f /etc/profile.d/go.sh ]; then
        echo -e "export GO111MODULE=${IS_GO111MODULE}\nexport GOCACHE=${GOCACHE_DIR}\nexport GOPATH=${GOPATH_DIR_1}:${GOPATH_DIR_2}\nexport GOPROXY=${GOPROXY_URL}\nexport PATH=\$PATH:$INSTALL_DIR/bin" | sudo tee /etc/profile.d/go.sh > /dev/null
        mkdir -p ${GOPATH_DIR_1} && mkdir -p ${GOPATH_DIR_2} && mkdir -p ${GOCACHE_DIR}
        source /etc/profile.d/go.sh
        echo "Environment variables updated."
    else
        echo "Go environment variables already exist."
    fi
}

# 验证安装
verify_installation() {
    echo "Checking installation..."
    go version
}

# 主函数
main() {
    detect_package_manager
    install_dependencies
    download_go
    extract_go
    cleanup
    update_environment
    verify_installation
}

# 运行主函数
main
